// -*- coding: utf-8 -*-

#include <iostream>
#include <fstream>
#include <string>

#include "hybrid_parser.h"
#include "hybrid_lexer.h"

HybridParser::HybridParser() 
  : m_lexer(new HybridLexer()),
    m_time(0.0, 3.0e0)
  //m_model(new AndModel())
{
	this->m_instance = this;

	//ConstPtr p(new IntervalConstant(Interval::Pi));
	IntervalConstant pi(Interval::Pi);
	ExprPtr p(new RealExpr(pi));
	m_constMap["pi"] = p;
}

bool HybridParser::parseFile(const char *filename) {
	std::ifstream ifs(filename);
	if (!ifs.is_open()) {
		std::cerr << "file " << filename << " does not exist!" << std::endl;
		return false;
	}
	m_lexer->switch_streams(&ifs, 0);

	return parse();
}
	
bool HybridParser::parseString(const std::string& str) {
	std::istringstream ss(str);
	/*
	m_lexer->switch_streams(&ss, 0);

	elisa::Parser parser(*this);
	return (parser.parse() == 0);
	*/
	return parseStream(ss);
}

bool HybridParser::parseStream(std::istream& in) 
{
	m_lexer->switch_streams(&in, 0);

	return parse();
}

bool HybridParser::parse() 
{
	elisa::HParser parser(*this);
	//parser.set_debug_level(1);
	return (parser.parse() == 0);
}

void HybridParser::error(const elisa::location& l, const std::string& s) 
{
	std::cerr << l << ": " << s << std::endl;
}
void HybridParser::error(const std::string& s) 
{
	std::cerr << s << std::endl;
}


HybridParser *HybridParser::m_instance = 0;
